﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace DogDemo
{
    public class WorldManager
    {
        //temp
        private Texture2D spriteSheet;
        public TileMap tileMap;
        private Explorer player;
        private Collectable apple;
        private Mob anEnemy;
        ObjectCollection objectCollection;
        SpriteFont pericles14;
        public enum GameStates { TitleScreen, OpenWorld, Combat, GameOver }
        private static GameStates _gameState = GameStates.OpenWorld;
        //temp

        public static GameStates GameState
        {
            get { return _gameState; }
            set { _gameState = value;  }
        }

        public WorldManager(ContentManager Content)
        {
            spriteSheet = Content.Load<Texture2D>(@"Textures\SpriteSheet");
            pericles14 = Content.Load<SpriteFont>(@"Fonts\Pericles14");
            Camera.WorldRectangle = new Rectangle(0, 0, 1600, 1600);
            Camera.ViewPortWidth = 800;
            Camera.ViewPortHeight = 600;
            tileMap = new TileMap(spriteSheet);
            player = new Explorer(spriteSheet, new Rectangle(0, 64, 32, 32), 6, new Vector2(300, 300));
            apple = new Collectable(spriteSheet, new Rectangle(0, 128,32,32), new Vector2(350, 350), "apple");
            anEnemy = new Mob(spriteSheet, new Rectangle(0, 160, 32, 32), new Vector2(500, 500));
            objectCollection = new ObjectCollection();
            objectCollection.Add(player);
            objectCollection.Add(apple);
            objectCollection.Add(anEnemy);
            

            // Object Manager should go here....
        }

        public virtual void Update(GameTime gameTime)
        {
            switch (GameState)
            {
                case GameStates.OpenWorld:
                    objectCollection.DoUpdating(gameTime, GameState, ref tileMap);
                    break;
            }

            // Method to update all objects using OM should go here
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            switch (GameState)
            {
                case GameStates.OpenWorld:
                        tileMap.Draw(spriteBatch);
                        objectCollection.DoDrawing(spriteBatch, GameState);
                        spriteBatch.DrawString(pericles14, DeterminePlayerInventory(), new Vector2(Camera.ViewPort.Top+20, Camera.ViewPort.Y), Color.Black);
                    break;
                case GameStates.Combat:
                    objectCollection.DoDrawing(spriteBatch, GameState);
                    break;
            }
            // Same, a method should go here to call 1 draw for the whole object list
        }

        private string DeterminePlayerInventory()
        {
            string returnVal = "Inventory: ";
            if (player.CollectedItems.Count > 0)
            {
                returnVal += player.CollectedItems.First();
            }
            else
            {
                returnVal += "Nothing";
            }

            return returnVal;
        }
    }
}
